package com.superyee.commonlib.ui;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.transition.Explode;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

import com.superyee.commonlib.logic.BaseLogic;
import com.superyee.commonlib.observer.UiObserver;
import com.superyee.commonlib.utils.ATStackUtil;
import com.superyee.commonlib.utils.Density;
import com.superyee.commonlib.widgets.LoadingDialog;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 版权所有 2017 金泰节能科技有限公司,保留所有版权。<br>
 * 项目名：	农信 - Android客户端<br>
 * 项目描述：Activity基础类，提供一些公共的能力实现和一些抽象的方法。
 * 作者： jingchao 日期：2017-11-15
 */
public abstract class BaseActivity extends AppCompatActivity implements UiObserver {

    private LoadingDialog mLoadingDialog;
    public static AtomicInteger baseAction = new AtomicInteger(100);
    private int action;

    public int getAction() {
        if (action == 0) {
            action = baseAction.incrementAndGet();
        }
        return action;
    }

    public int makeAction(int logicAction) {
        return BaseLogic.makeAction(getAction(), logicAction);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        getWindow().setEnterTransition(new Explode());
        getWindow().setExitTransition(new Explode());
        super.onCreate(savedInstanceState);
        getWindow().setSharedElementsUseOverlay(false);
        Density.setCustomDensity(this);
        ATStackUtil.getInstance().pushActivity(this);
        setContentView(getLayoutId());
        addListener();
        initPerData(savedInstanceState);
        initView();
        initData();
    }


    @Override
    protected void onDestroy() {
        closeLoadingDialog();
        removeListener();
        ATStackUtil.getInstance().popActivity(this);
        super.onDestroy();
    }


    @Override
    public void finish() {
        hideInputMethod();
        closeLoadingDialog();
        removeListener();
        ATStackUtil.getInstance().popActivity(this);
        super.finish();
    }

    /**
     * 子类自己去初始化自己的layout
     */
    protected abstract int getLayoutId();

    /**
     * 界面初始化之前初始化数据
     */
    protected abstract void initPerData(Bundle savedInstanceState);

    /**
     * 初始化控件
     */
    protected abstract void initView();

    /**
     * 初始化数据
     */
    protected abstract void initData();


    /***
     * 注册网络请求监听
     */
    protected abstract void addListener();

    /**
     * 删除网络请求监听
     */
    protected abstract void removeListener();

    protected Dialog showLoadingDialog() {
        return showLoadingDialog(null, false);
    }

    /**
     * @return
     */
    protected Dialog showLoadingDialog(boolean cancelable) {
        return showLoadingDialog(null, cancelable);
    }

    protected Dialog showLoadingDialog(String msg) {
        return showLoadingDialog(msg, false);
    }

    /**
     * @param msg
     */
    protected Dialog showLoadingDialog(String msg, boolean cancelable) {
        if (isFinishing()) {
            return null;
        }
        if (mLoadingDialog == null) {
            mLoadingDialog = LoadingDialog.createLoadingDialog(this);
        }
        mLoadingDialog.setMessage(msg);
        mLoadingDialog.setCancelable(cancelable);
        mLoadingDialog.show();
        return mLoadingDialog;
    }

    protected void closeLoadingDialog() {
        LoadingDialog.closeDialog(mLoadingDialog);
    }

    public void hideInputMethod() {
        View view = getWindow().peekDecorView();
        if (view != null && view.getWindowToken() != null) {
            InputMethodManager imm = (InputMethodManager)
                    getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
        }
    }

    /**
     * 显示软键盘
     *
     * @param inputView
     */

    public void showInputMethod(EditText inputView) {
        if (inputView != null) {
            inputView.requestFocus();
            inputView.setSelection(inputView.getText().length());
            InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            imm.showSoftInput(inputView, InputMethodManager.SHOW_FORCED);
        }
    }
}
